import { applyDecorators, type Type } from '@nestjs/common'
import { ApiExtraModels, ApiOkResponse, getSchemaPath } from '@nestjs/swagger'
import { Page } from '../entity'

export const ApiResponsePage = <T extends Type>(model: T) => {
  return applyDecorators(
    ApiExtraModels(Page, model),
    ApiOkResponse({
      schema: {
        $ref: `#/components/schemas/${Page.name}«${model.name}»`,
        allOf: [
          { $ref: getSchemaPath(Page) },
          {
            properties: {
              records: {
                type: 'array',
                items: { $ref: getSchemaPath(model) },
              },
            },
          },
        ],
      },
    })
  )
}
